﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HZ.MBSM.DBUtility;
using System.Drawing;
public partial class Company_GoodsDeailJB : System.Web.UI.Page
{
    Int64 goodsid;
    public string strSql, status, iszx="none";
    Int64 Accountid_QY;

    protected void Page_Load(object sender, EventArgs e)
    {
        int _intProductAddStatus = 0;
        System.Collections.Generic.List<HZ.MBSM.DAL.AccountType> _AccountType = new System.Collections.Generic.List<HZ.MBSM.DAL.AccountType>();
        _AccountType.Add(HZ.MBSM.DAL.AccountType.enumCompany);

        if (HZ.MBSM.BLL.Login.CheckAssert(_AccountType))
        {
            goodsid = Convert.ToInt64(MsgBox.JieMi_Decrypt(Request.QueryString["id"].ToString()));
            if (!IsPostBack)
            {
                
                    //string goodscount;
                    HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];
                    if (_Account != null)
                    {
                        if (_Account.Sort == "0")
                        {
                            choose.Visible = false;
                        }
                        else
                        {
                            choose.Visible = true;
                        }
                        Accountid_QY = SysFun.ToLong(_Account.AccountId);
                       
                        string ComName = "";
                        Db db = new Db();
                        System.Data.IDataReader dr;
                        //取得该企业的名称

                        strSql = "select CompanyName,ProductAddStatus,lastyearPayDuty,companyRegistermeney from company where Accountid='" + Accountid_QY.ToString() + "'";
                        dr = db.DataReader(strSql);
                        while (dr.Read())
                        {
                            ComName = dr["CompanyName"].ToString();
                            _intProductAddStatus = SysFun.ToInt(dr["ProductAddStatus"].ToString());
                            txtFactor0.Text = dr["lastyearPayDuty"].ToString().Trim();
                            txtFactor1.Text = dr["companyRegistermeney"].ToString().Trim();
                        }
                        dr.Close();
                        //取得商品信息
                        strSql = @" select PriceonlyDJ,ispPerformcountryprice,PriceliscensePW, dbo.Chk_GetCompanyName(AccountId_SC) as companyname_sc,AccountId_SC,AccountId_TJR, GoodsId, goods.ProductId, GoodsName,  LicenseNumber, LicenseNumberDate, IsSurveyReport, SurveyReportId, SurveyReportDate,   ImportCode, ImportRegistDate, IsPrimitive, Pack, RetailPrice, CheckStatus, IsImport, Factor, PackUnit, Licence, LicenceDate, AccountId_SC, GoodsInstruction,  Material,  NoPass,  ImgUrl, memo4, memo5, shenhetime, IsDel, sh_discrible, ProductNameSM, AccountName_SHR, AccountName_TJR,   ischange,  QualitylevelsStandard, Qualitylevelstrustiness, QualitylevelsManage ,productname_gjsp,Outlookc_gjsp,Medicinemodel_gjsp,unit_gjsp,case isdaishuye when 1 then goodsOther_bc when 2 then goodsOther_bc else '' end as 包装材质 from Goods,Product where Goods.ProductId=Product.ProductId
                                  and goodsid='" + goodsid.ToString() + "'";
                        dr = db.DataReader(strSql);
                        while (dr.Read())
                        {

                            labProductCode.Text = dr["productid"].ToString();


                            labProductName.Text = SysFun.ToTrim(dr["productname_gjsp"].ToString());//商品通用名

                            string _strMedicinemodel = dr["Medicinemodel_gjsp"].ToString();
                            string _strOutlookc = dr["Outlookc_gjsp"].ToString();
                            labMedicinemodel.Text = _strMedicinemodel;
                            labOutlookc.Text = _strOutlookc;
                            labunit.Text = dr["unit_gjsp"].ToString();
                            if (_Account.Sort == "0")
                            {
                                txtCompany.Text = ComName;//生产企业名称
                                txtIsSalerId.Value = _Account.AccountId.ToString();
                            }
                            else
                            {
                                txtCompany.Text = dr["companyname_sc"].ToString();//生产企业名称
                                txtIsSalerId.Value = dr["AccountId_SC"].ToString();
                            }

                            status = "none";//默认不显示

                            if (dr["ispPerformcountryprice"].ToString()!="" && Convert.ToBoolean(dr["ispPerformcountryprice"]))//是否不执行国家价格
                            {
                                chkIsImport.Checked = true;
                                TextWJPW.Text = dr["PriceliscensePW"].ToString();
                                iszx = "block";
                                txtPriceonlyDJ.Text = dr["PriceonlyDJ"].ToString();
                            }


                            /********* goods部分********/
                            labbaoz.Text = dr["包装材质"].ToString();
                            txtProductNameSM.Text = dr["ProductNameSM"].ToString();
                            txtFactor.Text = dr["factor"].ToString();
                            txtLicenseNumber.Text = dr["LicenseNumber"].ToString();
                            if (dr["LicenseNumberDate"] != "" && !dr["LicenseNumberDate"].ToString().Equals("1900-1-1 0:00:00"))
                            {
                                txtLicenseNumberDate.Text = dr["LicenseNumberDate"].ToString();
                            }
                            txtGoodsName.Text = dr["goodsname"].ToString();
                            txtMaterial.Text = dr["Material"].ToString();
                            if (dr["IsSurveyReport"].ToString() != "")
                            {
                                chkIsSurveyReport.Checked = Convert.ToBoolean(dr["IsSurveyReport"].ToString());
                            }
                            else
                            {
                                chkIsSurveyReport.Checked = false;
                            }    
                            txtSurveyReportId.Text = dr["SurveyReportId"].ToString();
                            if (dr["SurveyReportDate"] != null && !dr["SurveyReportDate"].ToString().Equals("1900-1-1 0:00:00"))
                            {
                                txtSurveyReportDate.Text = dr["SurveyReportDate"].ToString();
                            }
                            rdoQualitylevelsStandard.SelectedIndex = Convert.ToInt32(dr["QualitylevelsStandard"].ToString());
                            rdoQualitylevelstrustiness.SelectedIndex = Convert.ToInt32(dr["Qualitylevelstrustiness"].ToString());
                            RadioQualitylevelsManage.SelectedIndex = Convert.ToInt32(dr["QualitylevelsManage"].ToString());
                            txtGoodsInstruction.Text = dr["GoodsInstruction"].ToString();
                        
                            if (dr["checkstatus"] != "" && (dr["checkstatus"].ToString() == "1" || dr["checkstatus"].ToString() == "3"))
                            {
                                pnl.Enabled = false;
                            }
                            else
                            {
                                labnopass.Text = dr["NoPass"].ToString();
                            }
                        }
                        db.Close();
                        dr.Close();
                        dr.Dispose();
                        //判断产品是否有审核通过
                        if (HZ.MBSM.BLL.Company.GoodsIsExistpass(_Account.AccountId.ToString()))
                        {
                            txtFactor0.Enabled = false; txtFactor1.Enabled = false;
                        }
                        //labProductAddStatus.Value = _intProductAddStatus.ToString();
                        //if (_intProductAddStatus != 1)
                        //{
                        //    Response.Write("<script language=JavaScript>alert('您暂时没有权限增加品种！如需帮助请于招标中心联系！！！');window.close();</script>");

                        //}

                 
                }
            }
        }
    }


    protected void BtnSave_Click(object sender, EventArgs e)
    {
        if (labProductAddStatus.Value == "1")
        {
            addGoods();
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Write("<script>window.opener.location.reload();window.close();</script>");
    }
    private void addGoods()
    {
        Int64 IsSalerId_dl = 0;
        DateTime ImportRegistDate = Convert.ToDateTime("1900-1-1");
        HZ.MBSM.Model.Goods goods = new HZ.MBSM.Model.Goods();
        HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];
        if (_Account != null)
        {

            Db db = new Db();

            int rowCun = 0;

            System.Data.IDataReader dr;
            strSql = "select count(GOODSID) as cun from dbo.GOODS where PRODUCTID='" + labProductCode.Text.Trim() + "'  AND  FACTOR= '" + SysFun.ToTrim(SysFun.ToInt(txtFactor.Text)) + "' AND ACCOUNTID_SC=" + txtIsSalerId.Value + " and goodsid<>" + goodsid.ToString();
            dr = db.DataReader(strSql);
            if (dr.Read())
            {
                rowCun = SysFun.ToInt(dr["cun"]);
            }
            dr.Close();
            dr.Dispose();
            db.Close();
            if (rowCun > 0)
            {
                Response.Write("<script language=JavaScript>alert('该商品在数据库中已经存在！！！');window.close();</script>");
            }
            else
            {
                if (chkIsImport.Checked)
                {
                    if (TextWJPW.Text.Trim() == "")
                    {
                        Response.Write("<script language=JavaScript>alert('请填写物价批文发文号！！！');</script>");
                        return;
                    }
                    if (txtPriceonlyDJ.Text.Trim() == "")
                    {
                        Response.Write("<script language=JavaScript>alert('请填写单独定价价格！！！');</script>");
                        return;
                    }
                    else
                    {
                        if (!SysFun.IsNumeric(txtPriceonlyDJ.Text.Trim()))
                        {
                            Response.Write("<script language=JavaScript>alert('价格请填写数字！！！');</script>");
                            return;
                        }
                    }
                }
                goods.ispPerformcountryprice = chkIsImport.Checked;
                goods.PriceliscensePW = TextWJPW.Text.Trim();
                goods.PriceonlyDJ = txtPriceonlyDJ.Text.Trim();
                goods.GoodsId = goodsid;
                goods.AccountId_SC = SysFun.ToLong(txtIsSalerId.Value.ToString());
                goods.AccountId_TJR = _Account.AccountId;//添加人编码
                goods.ProductId = SysFun.ToLong(labProductCode.Text.Trim());
                goods.AccountName_TJR = _Account.AccountName;
                goods.Factor = SysFun.ToInt(txtFactor.Text.ToString());//转换系数

                goods.GoodsInstruction = txtGoodsInstruction.Text.ToString();
                goods.GoodsName = txtGoodsName.Text.ToString();

                goods.IsSurveyReport = chkIsSurveyReport.Checked;

                goods.LicenseNumber = txtLicenseNumber.Text.ToString();
                if (txtLicenseNumberDate.Text != "")
                    goods.LicenseNumberDate = Convert.ToDateTime(txtLicenseNumberDate.Text.ToString());
                else
                    goods.LicenseNumberDate = Convert.ToDateTime("1900-1-1");

                goods.Material = txtMaterial.Text.ToString();
                goods.Pack = "";
              
                goods.QualityLevels = "3";

                if (txtSurveyReportDate.Text != "")
                    goods.SurveyReportDate = Convert.ToDateTime(txtSurveyReportDate.Text.ToString());
                else
                    goods.SurveyReportDate = Convert.ToDateTime("1900-1-1");
                goods.SurveyReportId = txtSurveyReportId.Text.ToString();

                goods.ProductNameSM = txtProductNameSM.Text;
                goods.QualitylevelsManage = RadioQualitylevelsManage.SelectedIndex;
                goods.QualitylevelsStandard = rdoQualitylevelsStandard.SelectedIndex;
                goods.Qualitylevelstrustiness = rdoQualitylevelstrustiness.SelectedIndex;


                //添加GMP信息
                HZ.MBSM.BLL.Goods goodsOperator = new HZ.MBSM.BLL.Goods();

                if (goodsOperator.updategoodsjb(goods))
                {
                    goodsOperator.updatecompanyjb(txtFactor0.Text.Trim(), txtFactor1.Text.Trim(), _Account.AccountId.ToString(),false);
                    HZ.MBSM.BLL.LogManage.RecordLoginDetail(_Account.AccountName, Request.UserHostAddress, "插入", "goods", goods.GoodsId + goods.GoodsName + goods.GroupId + goods.ImportCode + goods.Licence + goods.LicenseNumberDate + goods.Material + goods.NewMedicine + goods.NewMedicineDate + goods.Pack + goods.PackUnit + goods.ProductId + goods.QualityLevels + goods.RetailPrice, "../Magage/goodsadd.aspx");
                    Response.Write("<script>alert('保存成功！！！');window.close();</script>");
                }
                else
                {
                    Response.Write("<script>alert('保存失败！！！');</script>");
                }
            }
        }
        else
        {
            Response.Write("<script language=JavaScript>alert('您无权添加商品！！！');</script>");
        }
    }
}
